package com.example.skillcinema.ui.screens.main

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import com.example.skillcinema.R
import com.example.skillcinema.adapters.FootLoadStateAdapter
import com.example.skillcinema.adapters.GalleryListPagedAdapter
import com.example.skillcinema.databinding.FragmentGalleryListBinding
import com.example.skillcinema.db.models.Episode
import com.example.skillcinema.ui.screens.main.viewmodels.GalleryListViewModel
import com.google.android.flexbox.AlignItems
import com.google.android.flexbox.FlexDirection
import com.google.android.flexbox.FlexWrap
import com.google.android.flexbox.FlexboxLayoutManager
import com.google.android.flexbox.JustifyContent
import com.google.android.material.chip.Chip
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

@AndroidEntryPoint
class GalleryListFragment : Fragment() {
    private var _binding: FragmentGalleryListBinding? = null
    private val binding: FragmentGalleryListBinding
        get() = _binding!!
    private val viewModel: GalleryListViewModel by viewModels<GalleryListViewModel>()
    private var filmId: Int = 0

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            filmId = it.getInt(GALLERY_FILM_ID)
            viewModel.setId(filmId)
            viewModel.loadData()
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
    ): View? {
        _binding = FragmentGalleryListBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        (activity as? AppCompatActivity)?.setSupportActionBar(binding.toolbar)
        (activity as? AppCompatActivity)?.supportActionBar?.setDisplayHomeAsUpEnabled(true)
        (activity as? AppCompatActivity)?.supportActionBar?.setDisplayShowTitleEnabled(false)
        binding.toolbar.title = ""
        binding.toolbar.setNavigationOnClickListener {
            findNavController().navigateUp()
        }
        val adapter: GalleryListPagedAdapter =
            GalleryListPagedAdapter { imageUrl ->
                showImage(
                    imageUrl
                )
            }
        binding.galleryImageRecyclerView.adapter = adapter.withLoadStateFooter(
            FootLoadStateAdapter()
        )
        lifecycleScope.run {
            launch {
                viewModel.imageTypes.collect {
                    it.forEachIndexed { index, item ->
                        val chip: Chip = View.inflate(
                            binding.seasonChipGroup.context, R.layout.season_chip, null
                        ) as Chip
                        chip.text = item.toString()
                        if (index == 0) {
                            chip.isChecked = true
                            viewModel.setImageType(item)
                        }
                        chip.setOnClickListener {
                            viewModel.setImageType(item)
                        }
                        binding.seasonChipGroup.addView(chip)
                    }
                }
            }
            launch {
                viewModel.imagesPaged.collectLatest {
                    adapter.submitData(it)
                }
            }
        }
    }

    private fun showImage(imageUrl: String) {

    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    companion object {
        @JvmStatic
        fun newInstance(param1: Int) = GalleryListFragment().apply {
            arguments = Bundle().apply {
                putInt(GALLERY_FILM_ID, param1)
            }
        }

        const val GALLERY_FILM_ID = "gallery_film_id"
    }
}